package org.ht.demo.openapi;

import com.alibaba.fastjson.JSON;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.ht.demo.openapi.util.HttpRequest;
import org.ht.demo.openapi.util.Response;
import org.ht.demo.openapi.util.SimpleHttpRequest;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 鸿途科技
 * @see
 * @since 1.0
 */
public class GatewayRequest {

    private static final String base_url = "http://ip:8848/api/v1/device";
    private static HttpClient httpClient = HttpClientBuilder.create().build();
    
    
    public static void main(String[] args) {


		
	}

    /**
     * 设备功能调用测试
     */
    public static void invokeFunction(String functionId,String deviceId,HashMap<String, Object> rmap) {
        String url = base_url + "/" + deviceId + "/function/" + functionId;

        System.out.println(url+"::rmap::"+rmap.toString());
        HttpRequest request = new SimpleHttpRequest(url, httpClient);
        String body = JSON.toJSONString(rmap);

        request.headers(HeaderUtils.createHeadersOfJsonString(body));
        System.out.println("Headers:===========>" + HeaderUtils.createHeadersOfJsonString(body));
        request.requestBody(body);
        try {
            Response response = request.post();
            System.out.println(JSON.parse(response.asBytes()));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 获取设备属性测试
     */
    void getProperty(String deviceId) {

        String property = "temperature";//温度属性，属性在设备管理的设备型号中设置
        String url = base_url + "/" + deviceId + "/property/" + property;

        HttpRequest request = new SimpleHttpRequest(url, httpClient);
        request.headers(HeaderUtils.createHeadersOfParams(new HashMap<>()));
        System.out.println(HeaderUtils.createHeadersOfParams(new HashMap<>()));
        try {

            Response response = request.get();

            System.out.println(JSON.parse(response.asBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 设置设备属性测试
     */
    void writeProperties(String deviceId) {
        String url = base_url + "/" + deviceId + "/properties";

        System.out.println(url);
        HttpRequest request = new SimpleHttpRequest(url, httpClient);
        Map<String, Object> params = new HashMap<>();
        params.put("temperature", 50F);

        request.headers(HeaderUtils.createHeadersOfJsonString(JSON.toJSONString(params)));
        System.out.println("Headers:===========>" + HeaderUtils.createHeadersOfParams(params));
        request.requestBody(JSON.toJSONString(params));

        try {
            Response response = request.post();
            System.out.println(JSON.parse(response.asBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 批量激活/注销设备测试
     */
    void batchDeploy() {
        String url = base_url + "/_deploy";//激活
        //String url = base_url + "/_unDeploy";//注销

        HttpRequest request = new SimpleHttpRequest(url, httpClient);

        String body = "[\"test003\",\"test004\"]";
        request.headers(HeaderUtils.createHeadersOfJsonString(body));
        request.requestBody(body);

        try {
            Response response = request.post();
            System.out.println(JSON.parse(response.asBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量删除设备测试（只会删除状态为未激活的设备）
     */
    void batchDelete() {
        String url = base_url + "/_delete";

        HttpRequest request = new SimpleHttpRequest(url, httpClient);

        String body = "[\"test002\",\"test003\",\"test004\"]";
        request.headers(HeaderUtils.createHeadersOfJsonString(body));
        request.requestBody(body);

        System.out.println("Headers:===========>" + HeaderUtils.createHeadersOfJsonString(body));

        try {
            Response response = request.post();
            System.out.println(JSON.parse(response.asBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据设备ID类型和动态查询参数查询设备日志
     */
    void queryDeviceLogPost(String deviceId) {
        String url = base_url + "/" + deviceId + "/log/_query";

        System.out.println(url);
        HttpRequest request = new SimpleHttpRequest(url, httpClient);

        //String body = "{\"pageSize\":25,\"pageIndex\":0,\"where\":\"productId is 1236859833832701952\"}";
        String body = "{\"pageSize\":25,\"pageIndex\":0,\"terms\":[{\"column\":\"productId\",\"value\":\"1236859833832701952\"}]}";
        request.headers(HeaderUtils.createHeadersOfJsonString(body));
        request.requestBody(body);

        try {
            Response response = request.post();
            System.out.println(JSON.parse(response.asBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询设备列表测试
     */
    void getDeviceDetailPost() {
        String url = base_url + "/_query";

        System.out.println(url);
        HttpRequest request = new SimpleHttpRequest(url, httpClient);

        //String body = "{\"pageSize\":25,\"pageIndex\":0,\"where\":\"productId is 1236859833832701952\"}";
        String body = "{\"pageSize\":25,\"pageIndex\":0,\"terms\":[{\"column\":\"productId\",\"value\":\"1236859833832701952\"}]}";
        request.headers(HeaderUtils.createHeadersOfJsonString(body));
        request.requestBody(body);

        try {
            Response response = request.post();
            System.out.println(JSON.parse(response.asBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询设备事件测试
     */
    void queryDeviceEvents(String deviceId) {

        String eventId = "fire_alarm";

        String url = base_url + "/" + deviceId + "/event/" + eventId + "/_query";

        System.out.println(url);
        HttpRequest request = new SimpleHttpRequest(url, httpClient);

        String body = "{\n" +
                "              \"pageSize\":25,\n" +
                "              \"pageIndex\":0,\n" +
                "              \"terms\":[\n" +
                "                 {\n" +
                "                   \"column\":\"a_name\",\n" +
                "                   \"value\":\"鸿途科技\"\n" +
                "                 }\n" +
                "              ]\n" +
                "          }";

        request.headers(HeaderUtils.createHeadersOfJsonString(body));
        System.out.println("Headers:===========>" + HeaderUtils.createHeadersOfJsonString(body));
        request.requestBody(body);

        try {
            Response response = request.post();
            System.out.println(JSON.parse(response.asBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
